<?php
namespace App\Service\CommonDataService;
use App\Service\BaseService;
use App\Model\PssModel\ServiceConfig;
use App\Service\Code\Normal;
use Hyperf\DbConnection\Db;
use App\Model\PssModel\SupplierUseConfig;

/**
 * 获取上下游服务对接配置
 * @author chenshubo
 */
class ServerConfigDataService extends BaseService
{
    /**
     * 获取service config 服务功能
     * @param string $service_code
     * @param int $service_type
     * @param string $service_platform_value
     * @param bool $is_single_field
     */
    public function queryServiceConfig(string $service_code, int $service_type,string $service_platform_value,bool $is_single_field = true)
    {
        $select = [
            'service_platform_value',
            'service_third_value',
            'service_name'
        ];
        $where = [
            'service_code'              => $service_code,
            'service_type'              => $service_type,
            'service_platform_value'    => $service_platform_value,
        ];
        $row = ServiceConfig::instance()->info($where,$select);
        if(empty($row)){
            return Normal::returnCode(Normal::FAIL,"数据不存在");
        }
        if( $is_single_field === false ) {
            return Normal::returnCode(Normal::SUC,"获取成功",$row);
        }
        return Normal::returnCode(Normal::SUC,"获取成功",$row['service_third_value'] ?? '');
    }
    
    /**
     * 获取三方服务配置列表
     * @param string $serviceCode
     * @param int $servicType
     * @param int $limit
     * @param int $page
     */
    public function queryServiceConfigList(string $serviceCode, int $servicType, int $limit, int $page)
    {
        $limit      = $limit ?? 15;
        $page       = $page ?? 0;
        $fields = [
            'id as ID',
            'service_code as serviceCode',
            'service_type as serviceType',
            'service_platform_value as servicePlatformValue',
            'service_third_value as serviceThirdValue',
            'service_name as serviceName',
            'service_code_name as triName',
        ];
        if( empty($servicType) ) {
            return Normal::returnCode(Normal::FAIL, "业务类型不能为空");
        }
        
        $where[]= ['service_type','=',$servicType];
        $query = ServiceConfig::instance()
        ->where($where);
        if($serviceCode){
            $query->where('service_code',$serviceCode);
        }
        $pageInfo = $query
        ->orderBy('ID','desc')
        ->paginate($limit,$fields, 'page', $page,true)
        ->toArray();
        $list = [
            'count' => $pageInfo['total'],
            'data'  => $pageInfo['data'],
        ];
        return Normal::returnCode(Normal::SUC,"获取成功",$list);
    }
    /**
     * 获取三方服务service_code
     * @param int $serviceType
     */
    public function queryServiceCodeList(int $serviceType)
    {
        if( empty($serviceType) ) {
            return Normal::returnCode(Normal::FAIL, "业务类型不能为空");
        }
        $where[]= ['service_type','=',$serviceType];
        $list = SupplierUseConfig::instance()
        ->select([
            Db::raw('id as ID'),
            Db::raw('supplier_code as serviceCode'),
            Db::raw('supplier_name as serviceName'),
        ])->get();
        return Normal::returnCode(Normal::SUC,"获取成功",$list);
    }
    
    /**
     * 新增三方服务
     * @param string $serviceCode
     * @param int $serviceType
     * @param string $servicePlatformValue
     * @param string $serviceThirdValue
     * @param string $serviceName
     */
    public function addServiceConfig(string $serviceCode,int $serviceType,string $servicePlatformValue,string $serviceThirdValue,string $serviceName)
    {
        if( empty($serviceCode) ) {
            return Normal::returnCode(Normal::FAIL, "三方业务编码不能为空");
        }
        if( empty($serviceType) ) {
            return Normal::returnCode(Normal::FAIL, "三方业务类型不能为空");
        }
        if( empty($servicePlatformValue) ) {
            return Normal::returnCode(Normal::FAIL, "内部映射值不能为空");
        }
        if( empty($serviceThirdValue) ) {
            return Normal::returnCode(Normal::FAIL, "外部映射值不能为空");
        }
        if( empty($serviceName) ) {
            return Normal::returnCode(Normal::FAIL, "类型名称不能为空");
        }
        $service_code_name = "";
        $useConfig = SupplierUseConfig::instance()->where("supplier_code",$serviceCode)->first();
        if($useConfig){
            $service_code_name = $useConfig["supplier_name"];
        }
        $data = [
            'service_code'               => $serviceCode,
            'service_type'               => $serviceType,
            'service_platform_value'     => $servicePlatformValue
        ];
        $list = ServiceConfig::instance()->where($data)->first();
        if($list){
            return Normal::returnCode(Normal::FAIL, "该数据已存在，不能重复添加！");
        }
        $data["service_third_value"] = $serviceThirdValue;
        $data["service_name"]        = $serviceName;
        $data["service_code_name"]   = $service_code_name;
        $data["cd"]                  = time();
        $res  =  ServiceConfig::instance()->add($data);
        if($res){
            return Normal::returnCode(Normal::SUC, "添加成功！");
        }else{
            return Normal::returnCode(Normal::FAIL, "添加失败！");
        }
    }
    
    /**
     * 修改三方服务
     * @param int $ID
     * @param string $serviceCode
     * @param int $serviceType
     * @param string $servicePlatformValue
     * @param string $serviceThirdValue
     * @param string $serviceName
     */
    public function updateServiceConfig(int $ID,string $serviceCode,int $serviceType,string $servicePlatformValue,string $serviceThirdValue,string $serviceName)
    {
        if( empty($ID) ) {
            return Normal::returnCode(Normal::FAIL, "ID不能为空");
        }
        if( empty($serviceCode) ) {
            return Normal::returnCode(Normal::FAIL, "三方业务编码不能为空");
        }
        if( empty($serviceType) ) {
            return Normal::returnCode(Normal::FAIL, "三方业务类型不能为空");
        }
        if( empty($servicePlatformValue) ) {
            return Normal::returnCode(Normal::FAIL, "内部映射值不能为空");
        }
        if( empty($serviceThirdValue) ) {
            return Normal::returnCode(Normal::FAIL, "外部映射值为空");
        }
        if( empty($serviceName) ) {
            return Normal::returnCode(Normal::FAIL, "类型名称不能为空");
        }

        $service_code_name = "";
        $useConfig = SupplierUseConfig::instance()->where("supplier_code",$serviceCode)->first();
        if($useConfig){
            $service_code_name = $useConfig["supplier_name"];
        }
        
        $list = ServiceConfig::instance()->where("id",$ID)->first();
        if(empty($list)){
            return Normal::returnCode(Normal::FAIL, "该数据不存在，请核实！");
        }
        $data = [
            'service_code'               => $serviceCode,
            'service_type'               => $serviceType,
            'service_platform_value'     => $servicePlatformValue,
        ];
        $list = ServiceConfig::instance()->where($data)->first();
        
        if($list && $ID !=$list["id"]){
            return Normal::returnCode(Normal::FAIL, "该数据【{$serviceCode}-{$serviceType}-{$servicePlatformValue}】存在，请核实！");
        }
        $data["service_third_value"]    = $serviceThirdValue;
        $data["service_name"]           = $serviceName;
        $data["service_code_name"]   = $service_code_name;
        $data["cd"]                     = time();
        $res  =  ServiceConfig::instance()->where("id",$ID)->update($data);
        if($res){
            return Normal::returnCode(Normal::SUC, "修改成功！");
        }else{
            return Normal::returnCode(Normal::FAIL, "修改失败！");
        }
    }
}